package com.jiang.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author jiangjingbo
 * @version V1.0
 * @Project java2203
 * @Package com.jiang.controller
 * @Description:
 * @Date 2022/6/8 22:39
 */
@Controller
@RequestMapping("/upload")
public class MultipartFileTest {
    @RequestMapping("test1")
    public String test(){
        System.out.println("/*-/*-*/*-*/");
        return "fileupload";
    }

//    这是的source 必须何前端传来的name 值一样
    @RequestMapping("/test2")
    @ResponseBody
    public String Test(MultipartFile source, HttpServletRequest request) throws IOException {
//        获取文件的name
        String filename = source.getOriginalFilename();
//        获取文件大小
//        long size = source.getSize();
//        获取文件内容
        InputStream inputStream = source.getInputStream();
        //获取保存文件的路径
        String realPath = request.getServletContext().getRealPath("/upload");
        //保存到硬盘上
        source.transferTo(new File(realPath+"\\"+filename));
        return "http://localhost:8080/upload/" + filename;
    }
}
